package com.kantboot.system.user.dao.repository.entity;

import com.kantboot.system.user.domain.entity.SysToken;
import jakarta.transaction.Transactional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

public interface SysTokenRepository extends JpaRepository<SysToken,Long> {

    /**
     * 根据token查询
     * @param token token
     * @return token
     */
    SysToken findByToken(String token);

    /**
     * 根据token查询userId
     * @param token token
     * @return userId
     */
    @Query("SELECT userId FROM SysToken WHERE token = :token")
    Long findUserIdByToken(@Param("token") String token);

    /**
     * 删除所有过期时间小于当前时间的token
     * @return token
     */
    @Query("DELETE FROM SysToken WHERE gmtExpire < CURRENT_TIMESTAMP")
    void deleteAllByGmtExpireLessThanNow();

    /**
     * 根据token删除
     * @param token token
     */
    @Modifying
    @Transactional
    @Query("DELETE FROM SysToken WHERE token = :token")
    void deleteByToken(String token);

}
